var selectedIsvRow = '', selectedRow = '';
var isInit = true;

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoIsvBizPage();
        },
    }
});

function doIsvRefresh() {
    selectedIsvRow = '';
    var isvName = $('#isvName').val();
    $('#isvTable').bootstrapTable('refresh', {
        url: basePath + 'basic/isvBiz/pageFindIsvData?t=' + new Date().getTime(),
        query: {
            isvName : isvName,
        },
    });
}

function doRefresh() {
    selectedRow = '';
    var bizName = $('#bizName').val();
    var isvPid = '';
    if (selectedIsvRow && selectedIsvRow != '') {
        isvPid = selectedIsvRow.isvPid;
    }
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'basic/isvBiz/pageFindIsvBizData?t=' + new Date().getTime(),
        query: {
            bizName : bizName,
            isvPid : isvPid,
        },
    });
}

function getIsvParams(params) {
    var isvName = $('#isvName').val();
    return {
        isvName : isvName,
        pageSize : params.pageSize,
        pageNo : params.pageNumber
    };
}

function getParams(params) {
    var bizName = $('#bizName').val();
    var isvPid = defIsvBizInfo.isvPid;
    return {
        bizName : bizName,
        isvPid : isvPid,
        pageSize : params.pageSize,
        pageNo : params.pageNumber
    };
}

function openAddIsvBizPage() {
    if (!selectedIsvRow || selectedIsvRow == '') {
        toastr.error(common_no_choose_msg.format("添加业务"));
        return;
    }
    if (!selectedIsvRow.isvPid || selectedIsvRow.isvPid == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('add', 0, selectedIsvRow.isvPid, selectedIsvRow.isvName);
}

function openEditIsvBizPage() {
    if (!selectedRow|| selectedRow == '') {
        toastr.error(common_no_choose_msg.format("编辑"));
        return;
    }
    openEditPage('edit', selectedRow.id, selectedRow.isvPid, selectedIsvRow.isvName);
}

function openInfoIsvBizPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('info', selectedRow.id, selectedRow.isvPid, selectedIsvRow.isvName);
}

function openEditPage(command, id, isvPid, isvName) {
    var searchKey = $('#bizName').val();
    var searchIsvName = $('#isvName').val();
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'basic/isvBiz/editIsvBizPage?type=' + command + '&searchKey=' + searchKey + '&isIsvInfoExist=' + defIsvBizInfo.isIsvInfoExist +
        '&id=' + id + '&isvPid=' + isvPid + '&isvName=' + isvName + '&searchIsvName=' + searchIsvName + '&t=' + new Date().getTime() +
        '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function doDeleteIsvBiz() {
    if (!selectedRow|| selectedRow == '') {
        toastr.error(common_no_choose_msg.format("删除"));
        return;
    }
    swalToastr('操作提示', '删除会导致数据库变动，确定要删除【'+selectedRow.bizName+'】吗？', 'warning', deleteIsvBiz);
}

function deleteIsvBiz() {
    var postData = {
        id : selectedRow.id,
        isvPid : selectedRow.isvPid,
        bizCode : selectedRow.bizCode,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'basic/isvBiz/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if (data&&data.errorCode == 'EXIST'){
                toastr.error('该服务商业务关联了服务，无法直接删除');
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

function doChangeIsvBiz() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var str = '冻结会导致数据库变动，确定要冻结【'+selectedRow.bizName+'】吗？';
    if (selectedRow.status == 'disabled') {
        str = '恢复会导致数据库变动，确定要恢复【'+selectedRow.bizName+'】吗？';
    }
    swalToastr('操作提示', str, 'warning', changeIsvBiz);
}

function changeIsvBiz() {
    var status = selectedRow.status == 'disabled' ? 'enabled' : 'disabled';
    var postData = {
        id : selectedRow.id,
        isvPid : selectedRow.isvPid,
        bizCode : selectedRow.bizCode,
        status : status
    };
    $.ajax({
        type: 'post',
        url: basePath + 'basic/isvBiz/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if (data&&data.errorCode == 'EXIST') {
                toastr.error('该业务已关联了机构服务或专家服务，无法直接删除');
            } else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

//------------------------------------------------------------------------------列表数据初始化
function formatStatus(value, row, index) {
    if (value == 'enabled') {
        return '有效';
    }
    if (value == 'disabled') {
        return '无效';
    }
    return value;
}

function initCheck(value, row, index) {
    if (isInit && row.isvPid == defIsvBizInfo.isvPid) {
        setTimeout(function(){
            $("#isvTable > tbody > tr[data-index='" +index+"']").addClass("success");
        },200);
        isInit = false;
        selectedIsvRow = row;
    }
    return value;
}

$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
    if (row.status == 'enabled') {
        $('#_btnChangeState').html('冻结');
    } else {
        $('#_btnChangeState').html('恢复');
    }
});

$("#isvTable").on("click-row.bs.table",function(e,row,ele){
    $("#isvTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedIsvRow = row;
    doRefresh();
});